package message.types.data;

import java.io.IOException;

import message.Message;
import message.visitor.MessageVisitor;
import network.e2e.FrameReader;
import network.e2e.FrameWriter;
import stalker.Stalker;
import data.content.Content;
import data.content.ContentParser;

public class AddDataRequest extends Message {
  
  private final Content content_;
  
  /**
   * Constructs an add data request
   * @requires parameters not null
   * @param client the requester
   * @param destination the target
   * @param content the content of the data that's going to be written
   */
  
  public AddDataRequest(Stalker client, Stalker destination, Content content) {
    super(client, destination);
    assert (content != null);
    content_ = content;
  }
  
  /**
   * Retrieves an add data request from the net
   * @requires parameters not null
   * @param client the requester
   * @param destination the target
   * @param reader the content of the data that's going to be from the net
   */
  public AddDataRequest(Stalker client, Stalker destination, FrameReader reader) throws IOException {
    super(client, destination);
    assert (reader != null);
    
    content_ = ContentParser.retrieve(reader);
  }
  
  /**
   * Retrieves the content of the new message being requested
   */
  public Content getContent() {
    return content_;
  }
  
  @Override
  public <T> T accept(MessageVisitor<T> visitor) {
    return visitor.visit(this);
  }
  
  @Override
  public String toString() {
    return "AddDataRequest by " + this.getClient() + ": " + this.getContent();
  }
  
  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof AddDataRequest)) {
      return false;
    }
    AddDataRequest that = (AddDataRequest) obj;
    return this.getClient().equals(that.getClient()) &&
           this.getDestination().equals(that.getDestination()) &&
           this.getContent().equals(that.getContent());
  }

  @Override
  public void send(FrameWriter writer) throws IOException {
    ContentParser.send(this.getContent(), writer);
  }

}
